package com.zjdiepu.www.manage.api.third;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.enums.type.IdcardSideType;
import com.zjdiepu.www.base.lang.HttpClientException;
import com.zjdiepu.www.common.util.Base64Coder;
import com.zjdiepu.www.ibiz.third.IJuheProvinceService;
import com.zjdiepu.www.ibiz.third.net.IOcrIdcardService;
import com.zjdiepu.www.model.third.request.ali.AliOcrIdcardConfigure;
import com.zjdiepu.www.model.third.request.ali.AliOcrIdcardImage;
import com.zjdiepu.www.model.third.request.ali.AliOcrIdcardInput;
import com.zjdiepu.www.model.third.request.ali.AliOcrIdcardRequest;
import com.zjdiepu.www.model.third.response.OcrIdcardResponse;
import com.zjdiepu.www.util.CachedEnums;
import com.zjdiepu.www.util.ExceptionHelper;
import com.zjdiepu.www.util.ObjectUtil;

/**
 * 项目名称：dp-rest <br/> 
 * 类名称：OcrIdcardController <br/>  
 * 类描述： 身份证识别<br/>    
 * 创建人：xiongxiaotun <br/> 
 * 创建时间：2017年3月28日 上午9:23:16  <br/>   
 * 修改人：xiongxiaotun <br/> 
 * 修改时间：2017年3月28日 上午9:23:16   <br/>  
 * 修改备注：    <br/> 
 * @version V1.0   <br/>
 */
@RestController
@RequestMapping(value = "idcard", produces = "application/json;charset=UTF-8")
public class OcrIdcardController extends BaseController {
	
	 @Resource
	  protected IJuheProvinceService juheProvinceService;
	 @Resource
	 protected IOcrIdcardService<AliOcrIdcardRequest> ocrIdcardService;
	/**
	 * @author xiongxiaotun <br/>
	 * @Title: queryProvince  <br/>
	 * @Description: 阿里云市场：身份证识别<br/>
	 * @param juheProvinceExt
	 * @return
	 * @version V1.0  <br/>
	 * @throws HttpClientException 
	 * @throws IOException 
	 */
	@RequestMapping(value = "ocr", method = { RequestMethod.POST })
	public ApiResponse ocr(String idcardSideCode, @RequestPart MultipartFile uploadFile) throws HttpClientException, IOException {
		try {
			if(StringUtils.isBlank(idcardSideCode)) {
				ExceptionHelper.thow("OCR_ERROR_MSG_004");
			}
			IdcardSideType idcardSideType = (IdcardSideType)CachedEnums.getByCode(idcardSideCode, IdcardSideType.class);
			if(null == idcardSideType) {
				ExceptionHelper.thow("OCR_ERROR_MSG_001");
			}
			long size = uploadFile.getSize();
			if(size > 1572864) { //1.5M
				ExceptionHelper.thow("OCR_ERROR_MSG_003");
			}
			byte[] bytes = uploadFile.getBytes();
			if(ObjectUtil.isEmpty(bytes)) {
				ExceptionHelper.thow("OCR_ERROR_MSG_002");
			}
			AliOcrIdcardRequest ocrIdcardRequest = new AliOcrIdcardRequest();
			List<AliOcrIdcardInput> inputs = new ArrayList<AliOcrIdcardInput>();
			AliOcrIdcardInput input = new AliOcrIdcardInput();
			
			AliOcrIdcardImage image = new AliOcrIdcardImage();
    		image.setDataType(50);
    		image.setDataValue(Base64Coder.encodeLines(bytes));
    		
    		AliOcrIdcardConfigure configure = new AliOcrIdcardConfigure();
    		configure.setDataType(50);
    		configure.setDataValue(idcardSideType.getCode());
    		
    		input.setImage(image);
    		input.setConfigure(configure);
    		inputs.add(input);
    		ocrIdcardRequest.setInputs(inputs);
			OcrIdcardResponse ocrResponse = ocrIdcardService.ocr(ocrIdcardRequest);
			
			return ApiResponse.success(ocrResponse);
		} catch (SocketTimeoutException e) {
			//try n times again .
			return ApiResponse.fail("HTTP_ERROR_MSG_001", "");
		}
	}
}
